package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ljfiction.book.MyView.ContainsEmojiEditText;
import com.ljfiction.book.MyView.SuperButton;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.SoftHideKeyBoardUtil;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class ThanksToReplyActivity extends BaseActivity {

    @BindView(R.id.back_thanks)
    ImageView backThanks;
    @BindView(R.id.username_thanks)
    TextView usernameThanks;
    @BindView(R.id.thankstext)
    ContainsEmojiEditText thankstext;
    @BindView(R.id.send_ok)
    SuperButton sendOk;
    private String userId, userName, contexts;
    private String areaid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thanks_to_reply);
        ButterKnife.bind(this);
        SoftHideKeyBoardUtil.assistActivity(this);
        userName = getIntent().getStringExtra("userName");
        userId = getIntent().getStringExtra("userId");
        areaid = getIntent().getStringExtra("areaid");

        usernameThanks.setText(userName);
    }

    @OnClick({R.id.back_thanks, R.id.send_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_thanks:
                goBack();
                break;
            case R.id.send_ok:
                tothanks();
                break;
        }
    }

    private void tothanks() {
        contexts = thankstext.getText().toString();
        if (StringTool.isEmpty(thankstext.getText().toString())){
            contexts="谢谢你打赏的辣椒,爱你哦，么么哒！！！";
        }
        OkHttpUtils.post(HttpManager.IP + "/person-sendmessage")

                .params("userName", UserManageBean.userBean.getPseudonym())
                .params("sendUserId", userId)
                .params("sendName", userName)
                .params("messageContent", contexts)
                .params("areaid", areaid)
                .params("type", "1")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject js = new JSONObject(s);
                            if (js.getInt("returnCode") == 200) {
                                showToast(js.getString("msg"));
                                setResult(8);
                                goBack();
                            } else {
                                if (js.getInt("returnCode") == 400){
                                    Intent intent=new Intent(ThanksToReplyActivity.this,LoginActivity.class);
                                    startActivity(intent);
                                }
                                showToast(js.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
